package com.sanshengshui.iot.test;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

public class NioEventLoopDemo {

    public static void main(String[] args) throws Exception{
        NioEventLoopGroup parent = new NioEventLoopGroup(3);
        System.out.println(parent.toString());

        EventLoop eventLoop = parent.next();
        System.out.println(eventLoop.getClass()+"");
        System.out.println(eventLoop+"");


        parent.execute(new Task());
        parent.schedule(new DelayTask(),1, TimeUnit.SECONDS);
        parent.scheduleAtFixedRate(new PeriodTask("parent_001"),0,2, TimeUnit.SECONDS);
        parent.scheduleAtFixedRate(new PeriodTask("parent_002"),0,2, TimeUnit.SECONDS);

        Thread.sleep(3000);

        eventLoop.execute(new Task());
        eventLoop.schedule(new DelayTask(),1, TimeUnit.SECONDS);
        eventLoop.scheduleAtFixedRate(new PeriodTask("child_001"),0,2, TimeUnit.SECONDS);

        Thread.sleep(3000000);


    }

    private static class Task implements Runnable{
        @Override
        public void run(){
            System.out.println("task run,"+Thread.currentThread()+","+ LocalDateTime.now());
        }
    }

    private static class DelayTask implements Runnable{
        @Override
        public void run(){
            System.out.println("DelayTask run,"+Thread.currentThread()+ LocalDateTime.now());
        }
    }

    private static class PeriodTask implements Runnable{
        final String name;
        public PeriodTask(String name){
            this.name=name;

        }
        @Override
        public void run(){
            System.out.println("PeriodTask ["+name+"] run,"+Thread.currentThread()+ LocalDateTime.now());
        }
    }
}
